// 1.引入创建仓库方法
import { createSlice } from '@reduxjs/toolkit'
import axios from 'axios'


// 2.实例化仓库对象(子仓库)
const billListSlice = createSlice({
    name: 'billList',
    // 初始值
    initialState: {
      value: []
    },
    // 同步修改state数据的方法
    reducers: {
        // state:表示保存状态数据的对象
        // action:表示调用方法时传过来的参数(action.payload)
      setbillList: (state,action) => {
        state.value =action.payload
      }
    }
  })

// 3.解构actions里面的方法并导出
  export const { setbillList } = billListSlice.actions

// 异步方法：请求数据
const getBillList=()=>{
    return async (dispatch)=>{
        // let res =await axios.get("xxxxxxxxxx");
        let res={
            data:[
                {
                    "type":"pay",
                    "money":-88,
                    "date":"2022-10-24 10:36:42",
                    "useFor":"drinks",
                    "id":1
                },
                {
                    "type":"pay",
                    "money":-99,
                    "date":"2022-10-24 09:36:42",
                    "useFor":"longdistance",
                    "id":2
                },
                {
                    "type":"income",
                    "money":100,
                    "date":"2022-10-22 09:36:42",
                    "useFor":"bonus",
                    "id":3
                },
                {
                    "type":"pay",
                    "money":-15,
                    "date":"2022-09-24 09:36:42",
                    "useFor":"dessert",
                    "id":4
                },
                {
                    "type":"income",
                    "money":10000,
                    "date":"2022-09-15 09:36:42",
                    "useFor":"wage",
                    "id":5
                }
            ]
        }


        // 触发同步方法
        dispatch(setbillList(res.data))
    }
}


// 4.导出子仓库的reducer函数
let billListReducer=billListSlice.reducer
export default billListReducer

export {getBillList}

